package com.wanglv.test;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class ListAdapter extends BaseAdapter {
    private static final String TAG = "ListAdapter";
    List<User> users;
    Context context;

    public ListAdapter(List<User> users, Context context) {
        this.users = users;
        this.context = context;
    }

    @Override
    public int getCount() {
        return users!=null?users.size():0;
    }

    @Override
    public Object getItem(int i) {
        return users.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view==null){
            view= LayoutInflater.from(context).inflate(R.layout.listlayout,null);
        }
        TextView tv_name,tv_password,tv_sex;
        tv_name=view.findViewById(R.id.name);
        tv_password=view.findViewById(R.id.password);
        tv_sex=view.findViewById(R.id.sex);
        Log.d(TAG, "getView: "+users.get(i));
        tv_name.setText(users.get(i).getUsername());
        tv_password.setText(users.get(i).getPassword());
        tv_sex.setText(users.get(i).getSex());
        return view;
    }
}
